package com.it.mybatisplus.springbootmybatisplu.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.it.mybatisplus.springbootmybatisplu.beans.bo.TestBO;
import com.it.mybatisplus.springbootmybatisplu.model.Test;
import com.it.mybatisplus.springbootmybatisplu.service.TestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 通过mybatis2.3x，生成curd来执行数据库
 */
@RestController
@RequestMapping("/test")
public class test {
    @Autowired
    private TestService testService;
    @RequestMapping("/addTest")
    public String addTest(@RequestBody TestBO testBO){
        Test test = new Test();
        BeanUtils.copyProperties(testBO,test);
        testService.insert(test);
        return "ok";
    }
    @RequestMapping("/delTest")
    public String delTest(@RequestBody TestBO testBO){
        Test test = new Test();
        BeanUtils.copyProperties(testBO,test);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("id",testBO.getId());
        testService.delete(entityWrapper);
        return "ok";
    }
    @RequestMapping("/queryTest")
    public String queryTest(@RequestBody TestBO testBO){

        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("id",testBO.getId());
       List<Test> list =  testService.selectList(entityWrapper);

        return JSONUtil.parse(list).toString();
    }
    @RequestMapping("/updateTest")
    public String updateTest(@RequestBody TestBO testBO){
        Test test = new Test();
        BeanUtils.copyProperties(testBO,test);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("id",testBO.getId());
        testService.update(test,entityWrapper);
        return "ok";
    }
}
